package cn.bus.service.impl;

import cn.bus.domain.Car;
import cn.bus.domain.Rent;
import cn.bus.mapper.CarMapper;
import cn.bus.mapper.RentMapper;
import cn.bus.service.RentService;
import cn.bus.vo.RentVo;
import cn.sys.utils.DataGridView;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Mxb
 * @version 1.0
 * @date 2019/11/4 15:12
 */
@Service
public class RentServiceImpl implements RentService {

    @Autowired
    private RentMapper rentMapper;

    @Autowired
    private CarMapper carMapper;

    //根据出租单号 查询出租信息
    @Override
    public Rent queryRentByRentId(String id) {
        return rentMapper.selectByPrimaryKey(id);
    }

    //保存出租单信息
    @Override
    public void addRent(RentVo rentVo) {
        this.rentMapper.insertSelective(rentVo);
        //更改车辆的出租状态
        Car car = new Car();
        car.setCarnumber(rentVo.getCarnumber());
        //  0：未出租  1：出租中
        car.setIsrenting(1);
        carMapper.updateByPrimaryKeySelective(car);
    }

    /*************************************************************************************************/
    //查询所有出租单
    @Override
    public DataGridView queryAllRent(RentVo rentVo) {
        Page<Object> page = PageHelper.startPage(rentVo.getPage(), rentVo.getLimit());
        List<Rent> data = this.rentMapper.queryAllRent(rentVo);
        return new DataGridView(page.getTotal(), data);
    }

    @Override
    public void updateRent(RentVo rentVo) {
        this.rentMapper.updateByPrimaryKeySelective(rentVo);
    }

    @Override
    public void deleteRent(String rentid) {
        //更改汽车状态
        Rent rent = this.rentMapper.selectByPrimaryKey(rentid);
        Car car = new Car();
        car.setCarnumber(rent.getCarnumber());
        //  0：未出租  1：出租中
        car.setIsrenting(0);
        carMapper.updateByPrimaryKeySelective(car);
        //删除出租单
        this.rentMapper.deleteByPrimaryKey(rentid);
    }
}
